
#---------------------------------------------------------------------------------------------------------------
function Resolve-Value ($val, $default)
{
	if ($val -ne $null -and $val -ne "") {$val}
	else {$default}
}

function DebugDisplayParms($msg)
{
	Log-Warning "-------------------------------------------------------------------------"
	Log-Warning $msg
	Log-Message "Database Deployment parameters"
	Log-Message "        Provider: $Provider"
	Log-Message "      ServerName: $ServerName"
	Log-Message "    DatabaseName: $DatabaseName"
	Log-Message "        UserName: $Username"
	Log-Message "        Password: $Password"
	Log-Message "   ProviderParms: $XtraProviderParms"
	Log-Message "     PatchGroups: $PatchGroups"
}

#DebugDisplayParms "Initial Values"

#---------------------------------------------------------------------------------------------------------------
if ($ConfigFile -eq $null -and ($TargetEnv -eq $null))
{
	$ConfigFile = (Join-Path Get-ScriptDir "DBDeploy.config")
}
elseif(-not($ConfigFile -eq $null) -and (-not ($TargetEnv -eq $null)))
{
$deploymentroot = Get-Location

$ConfigFile = Join-Path $deploymentroot $ConfigFile
}
if (($ConfigFile -eq $null) -or (-not (Test-Path $ConfigFile)))
{
	Throw "ConfigFile $ConfigFile not found"
}

#---------------------------------------------------------------------------------------------------------------
$DBConfigFile = [system.IO.FileInfo]$ConfigFile

log-message $MinorLine
log-message "Database Deployment"
log-message $MinorLine
Write-Host `r
Log-Message "Reading DBConfig File $($DBConfigFile.Fullname)"
$config = Read-DBDeployConfig $DBConfigFile.Fullname

$Provider = $config.Provider
$SchemaDir = (Get-FullPath (Join-Path $DBConfigFile.Directory.FullName $config.RootDirectory))

#---------------------------------------------------------------------------------------------------------------
if (([string]$EnvironmentName) -eq "")
{
	$EnvironmentName = $config.TargetEnvironments.Default
}

if (([string]$EnvironmentName) -eq "")
{
	Log-Error "Target Environmentment is unknown.  Use -EnvironmentName to specify or add a default to TargetEnvironments in $ConfigFile"
}

#---------------------------------------------------------------------------------------------------------------
Log-Message "Using Configured TargetEnvironment $EnvironmentName"
$Environment = $config.TargetEnvironments.GetTargetEnvironment($EnvironmentName)

#---------------------------------------------------------------------------------------------------------------
# If $DefaultDatabaseProviderParms exist, Use it's values unless override by params
if ($DefaultDatabaseProviderParms -ne $null)
{
	$ServerName = Resolve-Value $ServerName $DefaultDatabaseProviderParms[0]
	$DatabaseName = Resolve-Value $DatabaseName $DefaultDatabaseProviderParms[1]
	$Username = Resolve-Value $Username $DefaultDatabaseProviderParms[2]
	$Password = Resolve-Value $Password $DefaultDatabaseProviderParms[3] 
	$XtraProviderParms = Resolve-Value $XtraProviderParms $DefaultDatabaseProviderParms[4]
}
#DebugDisplayParms "Post DefaultDatabaseProviderParms"

#---------------------------------------------------------------------------------------------------------------
# Use $Environment values unless override by params or from $DefaultDatabaseProviderParms

$ServerName = Resolve-Value $ServerName $Environment.ServerName
$DatabaseName = Resolve-Value $DatabaseName $Environment.DatabaseName
$Username = Resolve-Value $Username $Environment.Username 
$Password = Resolve-Value $Password $Environment.Password 
$XtraProviderParms = Resolve-Value $XtraProviderParms $Environment.XtraProviderParms 
if ([string]$Environment.PatchGroups -ne '')
{
	$PatchGroups = Resolve-Value $PatchGroups ($Environment.PatchGroups.Split(',')) 
}
else
{
	$PatchGroups = Resolve-Value $PatchGroups "ALL" 
}


#DebugDisplayParms "Post Environment"

#---------------------------------------------------------------------------------------------------------------
Log-Message -Level Verbose "Database Deployment parameters"
Log-Message -Level Verbose "        Provider: $Provider"
Log-Message -Level Verbose "      ServerName: $ServerName"
Log-Message -Level Verbose "    DatabaseName: $DatabaseName"

if ($Username -ne "")          {Log-Message -Level Verbose "        UserName: $Username"}
if ($Password -ne "")          {Log-Message -Level Verbose "        Password: **********"}
if ($XtraProviderParms -ne "") {Log-Message -Level Verbose "   ProviderParms: $XtraProviderParms"}
if ($PatchGroups -ne "")       {Log-Message -Level Verbose "     PatchGroups: $PatchGroups"}

#---------------------------------------------------------------------------------------------------------------
$ProviderParms=@($ServerName,$DatabaseName,$Username,$Password,$XtraProviderParms)

$DbDeployimentParmsResolved=$TRUE

